﻿
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MT5Collect
{
    public class DBHelper
    {
        #region MYSQL
        public static string connectionString;

        /// <summary>
        /// 测试连接
        /// </summary>
        public static bool Connect()
        {
            MySqlConnection connection = new MySqlConnection(connectionString);
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            try
            {
                connection.Open();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        public static bool Connect(string conn)
        {
            MySqlConnection connection = new MySqlConnection(conn);
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            try
            {
                connection.Open();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable ExecuteQuery(string sql)
        {
            try
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand command = new MySqlCommand(sql, connection))
                    {
                        connection.Open();
                        using (MySqlDataAdapter adapter = new MySqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            return dt;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql)
        {
            try
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand command = new MySqlCommand(sql, connection))
                    {
                        connection.Open();
                        return command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception)
            {
                string a = sql;
                throw;
            }
           
        }

        #endregion
    }
}
